import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/material.dart';

class IconFollowMenu extends StatefulWidget {
  final Widget child;
  final EdgeInsets padding;
  final ValueChanged<String> onSelect;

  const IconFollowMenu({Key key, this.child, this.padding, this.onSelect})
      : super(key: key);

  @override
  _IconFollowMenuState createState() => _IconFollowMenuState();
}

class _IconFollowMenuState extends State<IconFollowMenu> {
  OverlayEntry _overlayEntry;
  LayerLink _link = LayerLink();

  @override
  Widget build(BuildContext context) {
    return CompositedTransformTarget(
      link: _link,
      child: PressedOpacityButton(
        onPressed: onShowFollow,
        padding: widget.padding,
        child: widget.child,
      ),
    );
  }

  void onDismiss() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }

  void onShowFollow() {
    if (_overlayEntry != null) {
      onDismiss();
    } else {
      showFollower();
    }
  }

  void showFollower() {
    _overlayEntry = OverlayEntry(builder: (BuildContext context) {
      return GestureDetector(
        onTap: onDismiss,
        child: UnconstrainedBox(
            child: CompositedTransformFollower(
          link: _link,
          followerAnchor: Alignment.bottomLeft,
          targetAnchor: Alignment.topCenter,
          child: Material(
            child: Container(
              alignment: Alignment.center,
              width: getDp(166),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(getDp(5)),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.25),
                      blurRadius: getDp(4),
                    )
                  ]),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  S.current.camera,
                  S.current.photo_library,
                ]
                    .map((e) => PressedOpacityButton(
                          onPressed: () {
                            onDismiss();
                            widget.onSelect?.call(e);
                          },
                          padding: EdgeInsets.symmetric(
                            horizontal: getDp(18),
                            vertical: getDp(19),
                          ),
                          child: Text(
                            e,
                            style: TextStyle(
                              color: aceBlackColor,
                              fontSize: getSp(16),
                              fontWeight: FontWeight.w400,
                              height: 1,
                            ),
                          ),
                        ))
                    .toList(),
              ),
            ),
          ),
        )),
      );
    });
    Overlay.of(context).insert(_overlayEntry);
  }

  @override
  void dispose() {
    super.dispose();
    onDismiss();
  }
}
